﻿using System;
using System.Configuration;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using GanagroLiteData;
using Csla.Validation;
using GeneralUtilities;

namespace GanagroLiteBO
{

public partial class PARAMETRO_BO : IEditableObject, INotifyPropertyChanged, IDataErrorInfo 
{private bool m_Editing;

#region Constructors
public PARAMETRO_BO()
	{
	}
#endregion
  #region Private Fields
	private int _ITEM;	
	private string _DESCRIPCION;	
	private string _VALOR;	
  #endregion

    #region Public Properties
	public int ITEM
	{
		get { return _ITEM; }
		set { _ITEM = value;
		 FirePropertyChangedNotification("ITEM"); }
	}
	public string DESCRIPCION
	{
		get { return _DESCRIPCION; }
		set { _DESCRIPCION = value;
		 FirePropertyChangedNotification("DESCRIPCION"); }
	}
	public string VALOR
	{
		get { return _VALOR; }
		set { _VALOR = value;
		 FirePropertyChangedNotification("VALOR"); }
	}
    #endregion

    #region validations
    private ValidationRules _validationRules;
    public ValidationRules ValidationRules
    {
        get
        {
            if (_validationRules == null)
                _validationRules = new ValidationRules(this);
            return _validationRules;
        }
    }

    public bool Isvalid()
    {
        ValidationRules.CheckRules();
        return ValidationRules.GetBrokenRules().ErrorCount == 0;
    }

    #endregion

    #region INotifyPropertyChanged Implementation

    protected void FirePropertyChangedNotification(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        ValidationRules.CheckRules(propName);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    #endregion

    #region IDataErrorInfo Members

    [Browsable(false)]
    public string Error
    {
        get
        {
            if (!ValidationRules.IsValid)
            { return ValidationRules.GetBrokenRules().ToString(Csla.Validation.RuleSeverity.Error); }
            else
            { return String.Empty; }
        }
    }

    [Browsable(false)]
    public string this[string columnName]
    {
        get
        {
            string result = string.Empty;
            if (!ValidationRules.IsValid)
            {
                BrokenRule rule = ValidationRules.GetBrokenRules().GetFirstBrokenRule(columnName);
                if (rule != null)
                    result = rule.Description;
            }
            return result;
        }
    }

    #endregion

    #region IEditableObject Members

    public void BeginEdit()
    {
        if (!m_Editing)
        {
        }

        m_Editing = true;
    }

    public void CancelEdit()
    {
        m_Editing = false;
    }

    public void EndEdit()
    {
        if (m_Editing)
        {
        }

        m_Editing = false;
    }

#endregion

}

public class Parametros : BindingListView<PARAMETRO_BO> 
 {
    public Parametros()
    {
        using (GlContext glContext = new GlContext())
        {
            foreach (var item in glContext.PARAMETROS.OrderBy(p => p.ITEM))
            {
                PARAMETRO_BO pbo = new PARAMETRO_BO();
                pbo.DESCRIPCION = item.DESCRIPCION;
                pbo.ITEM = item.ITEM;
                pbo.VALOR = item.VALOR;
                this.Add(pbo);
            }
        }
    }

    public string GetVal(Pars key)
    {
        try
        {
            return this.First(p => (Pars)p.ITEM == key).VALOR;
        }
        catch
        {
            return string.Empty;
        }
    }

    public void Guardar()
    {
        using (GlContext glContext = new GlContext())
        {
            foreach (var item in this)
            {
                PARAMETRO P = glContext.PARAMETROS.First(p => p.ITEM == item.ITEM); //new PARAMETRO(p => p.ITEM == item.ITEM);
                glContext.PARAMETROS.Attach(P);
                glContext.Entry(P).State = System.Data.Entity.EntityState.Modified;
                P.VALOR = item.VALOR;
            }
            glContext.SaveChanges();
        }
        utilities.parametrosGanagro = null;
    }
 }

}